package net.lahuobao.mall.service.impl.admin;

import net.lahuobao.mall.entity.admin.MallEtcCardRuleEntity;
import net.lahuobao.mall.entity.admin.MallShopCardruleEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.lahuobao.mall.service.admin.MallEtcCardRuleServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("mallEtcCardRuleService")
@Transactional
public class MallEtcCardRuleServiceImpl extends CommonServiceImpl implements MallEtcCardRuleServiceI {
    @Override
    public void save(MallEtcCardRuleEntity mallEtcCardRule) {
        //计算ETC卡
        Integer startCardNo = Integer.valueOf(mallEtcCardRule.getStartCardNo());
        Integer endCardNo = Integer.valueOf(mallEtcCardRule.getEndCardNo());
        List<MallEtcCardRuleEntity> cardModelList = new ArrayList<MallEtcCardRuleEntity>();

        int length = endCardNo.toString().length();
        for (int i = startCardNo; i <= endCardNo; i++) {
            MallEtcCardRuleEntity param = new MallEtcCardRuleEntity();
            //创建人
            param.setCreateuserid(mallEtcCardRule.getCreateuserid());
            //创建时间
            param.setCreatetime(new Date());
            //店铺
            param.setShopid(mallEtcCardRule.getShopid());
            //供应商
            param.setEtcsupplierid(mallEtcCardRule.getEtcsupplierid());
            //商品
            param.setProductid(mallEtcCardRule.getProductid());
            //状态 --0未使用
            param.setState(0);
            //卡号
            String cardNo = mallEtcCardRule.getCardNoPrefix();
            //长度不相等，补位
            String temp = String.valueOf(i);
            if (String.valueOf(i).length() != String.valueOf(endCardNo).length()) {
                String str ="0000000000";
                Integer tempLength =  String.valueOf(endCardNo).length() - String.valueOf(i).length();
                cardNo += str.substring(0, tempLength) + i;
            }else{
                cardNo = cardNo + i;
            }
            param.setCardno(cardNo);
            cardModelList.add(param);
        }
        super.batchSave(cardModelList);
    }
}